<?php 
/**
*
* example目录下为简单的支付样例，仅能用于搭建快速体验微信支付使用
* 样例的作用仅限于指导如何使用sdk，在安全上面仅做了简单处理， 复制使用样例代码时请慎重
* 请勿直接直接使用样例对外提供服务
* 
**/
namespace App\Tools;

use App\Models\Order;
use App\Models\Shop;
use App\Models\ShopRoomTable;
use App\Models\WeixinUser;

class WechatSend{

    // 发送模板消息
    public function orderTemplate($order_id){//支付成功给商家微信消息
        try {
            //验证订单
            $order = Order::find($order_id);
            if (!$order) {
                return;
            }
            //验证商家
            $shop = Shop::find($order->shop_id);
            if (!$shop) {
                return;
            }
            //获取用户
            $wechatuser = WeixinUser::where('user_id', $shop->user_id)->first();
            if (!$wechatuser) {
                return;
            }
            if ($wechatuser->openid == null) {
                return;
            }
            //处理模板
            if ($order->order_type == 0) {
                $title = '请在十分钟内接单';
                $type = '外卖';
                $money = $order->order_amount;
            } else if ($order->order_type == 1) {
                if ($order->order_status == 2) {
                    $title = '收到一个预定订单';
                    $type = '预定';
                    $money = $order->order_amount;
                } else if ($order->order_type == 4) {
                    $title = '该订单已结账';
                    $type = '预定';
                    $money = $order->order_price;
                } else {
                    return;
                }
            } else if ($order->order_type == 2) {
                $title = '收到排队订单';
                $type = '排队';
                $money = $order->order_amount;
            } else if ($order->order_type == 3) {
                $title = '收到扫码订单';
                $type = '扫码';
                $money = $order->order_amount;
            } else {
                return;
            }
            $url = '/mobile/shopAdmin/enewstore?id=' . $order_id . '&order_type=' . $order->order_type;
            $app = app('wechat.official_account');
            $ees = $app->template_message->send([
                'touser' => $wechatuser->openid,
                'template_id' => 'cIw5Sv5ZyZvjDmZvnkwRoPK365GuR9QMRA2FbIHUpVc',
                'url' => env('APP_URL') . $url,
                'data' => [
                    'title' => $title,
                    'number' => $order->order_number,
                    'type' => $type,
                    'money' => $money,
                    'time' => $order->updated_at
                ],
            ]);
        }catch (\Exception $e){
            return;
        }
    }

    // 小程序发送模板消息
    public function sendMiniReserveTemplate($order_id){
        try{
            //验证订单
            $order = Order::find($order_id);
            if (!$order) {
                return;
            }
            //验证商家
            $shop = Shop::find($order->shop_id);
            if (!$shop) {
                return;
            }
            //获取用户
            $wechatuser = WeixinUser::where('user_id', $shop->user_id)->first();
            if (!$wechatuser) {
                return;
            }
            if ($wechatuser->openid_mini == null) {
                return;
            }
            $mini = app('wechat.mini_program');
            $res = $mini->template_message->send([
                'touser' =>$wechatuser->openid_mini,
                'template_id' => 'XeF1_YL6Y0OTvZmW_7JMAYB85AaofvNj64GptHMYiN4',
                'page' => 'index',
                'form_id' =>$order->prepay_id,
                'data' => [
                    'keyword1' => $order->order_number,  // 订单号
                    'keyword2' => $shop->shop_name, // 餐厅名称
                    'keyword3' =>$order->created_at, //  预定时间
                    'keyword4' => $order->order_price, // 预定定金
                    'keyword5' => '联系电话', // 联系电话
                    'keyword6' => '桌子号', // 桌号
                    'keyword7' => '地点', // 地点
                    'keyword8' => '姓名', //姓名
                    'keyword9' => $order->note, // 备注
                ],
            ]);
            return $res;
        }catch (\Exception $e){
            dd($e->getMessage());
        }
    }

}

